package com.bishe.controller;


import cn.hutool.core.util.StrUtil;
import com.bishe.pojo.Message;
import com.bishe.pojo.User;
import com.bishe.serivce.IMessageService;
import com.bishe.serivce.IUserService;
import com.bishe.utils.EcoBootException;
import com.bishe.utils.Result;
import com.bishe.utils.SqlQueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin")
@Api(tags = "管理员")
@CrossOrigin
public class AdminController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IMessageService messageService;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @ApiOperation("管理员添加修改用户")
    @PostMapping("/admin/add")
    public Result addUser(@RequestBody User user){
        if (user == null){
            throw new EcoBootException("数据不能为空");
        }
        SqlQueryUtils<User> queryUtils = new SqlQueryUtils<>(User.class,jdbcTemplate);
        // 根据name查询单条记录
        User one = queryUtils.eq(User::getId, user.getId()).getOne();

        //设置创建时间
        user.setCreateTime(new Date());
        // 不存在 添加
        if (one == null){
            user.setId(StrUtil.uuid());
            userService.save(user);
            return Result.ok("添加成功");
        }else{
            userService.updateById(user);
            return Result.ok("修改成功");
        }
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/admin/delete/{id}")
    public Result deleteById(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }

        userService.deleteById(id);
        return Result.ok("删除成功");
    }

    @ApiOperation("添加消息")
    @PostMapping("/add")
    public Result addMessage(@RequestBody Message message){
        if (message == null){
            throw new EcoBootException("数据不能为空");
        }
        SqlQueryUtils<Message> queryUtils = new SqlQueryUtils<>(Message.class,jdbcTemplate);
        // 根据name查询单条记录
        Message one = queryUtils.eq(Message::getId, message.getId()).getOne();

        message.setMessageTime(new Date());
        // 不存在 添加
        if (one == null){
            message.setCreateTime(new Date());
            message.setMessageTime(new Date());
            message.setId(StrUtil.uuid());
            messageService.save(message);
            return Result.ok("添加成功");
        }else{
            messageService.updateById(message);
            return Result.ok("修改成功");
        }

    }

    @ApiOperation("用户列表")
    @GetMapping("/getList")
    public Result getUserList(){

        List<User> list = new SqlQueryUtils<>(User.class, jdbcTemplate).orderByDesc(User::getCreateTime).getList();

        List<User> resultList = list.stream().filter(s -> !"admin".equals(s.getUserName())).collect(Collectors.toList());
        for (User user : resultList) {
            user.setPassWord("***");
        }
        return Result.ok(resultList);
    }
}
